package it.kion.util.ui.umenumanager2.parser;

import java.io.IOException;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class DesktopMaker {

	public DesktopMaker() {

		Element root = new Element("desktop", "http://www.cineca.it/sia/desktop/desktop.xsd");
		Element menu = new Element("menu");
		menu.setAttribute("id", "main");
		root.addContent(menu);

		// <label>Menu Principale</label>
		Element label = new Element("label");
		label.setText("Menu Principale");
		menu.addContent(label);

		// <description>Menu Principale</description>
		Element description = new Element("description");
		description.setText("Menu Principale");
		menu.addContent(description);

		// <icon>/folder.gif</icon>
		Element icon = new Element("icon");
		icon.setText("/folder.gif");
		menu.addContent(icon);

		addUserContextElement(menu);

		Document doc = new Document(root);
		// serialize it onto System.out
		try {
			XMLOutputter serializer = new XMLOutputter(Format.getPrettyFormat());
			serializer.output(doc, System.out);
		}
		catch (IOException e) {
			System.err.println(e);
		}

	}

	/*
	 * Aggiunge al desktop.xml il seguente elemento: <user-context> <user-context-class>it.cineca.sia.frk.commons.context.UserContext</user-context-class>
	 * <business-process>it.cineca.sia.desktop.bp.UserContextBP</business-process> <configure-view-id>/desktop/usercontext.jsf</configure-view-id>
	 * <view-id>/desktop/usercontext.jsp</view-id> </user-context>
	 */
	private void addUserContextElement(Element menu) {

		// <user-context>
		Element user_context = new Element("user-context");

		// <user-context-class>it.cineca.sia.frk.commons.context.UserContext</user-context-class>
		Element user_context_class = new Element("user-context-class");
		user_context_class.addContent("it.cineca.sia.frk.commons.context.UserContext");

		// <business-process>it.cineca.sia.desktop.bp.UserContextBP</business-process>
		Element business_process = new Element("business-process");
		business_process.addContent("it.cineca.sia.desktop.bp.UserContextBP");

		// <configure-view-id>/desktop/usercontext.jsf</configure-view-id>
		Element configure_view_id = new Element("configure-view-id");
		configure_view_id.addContent("/desktop/usercontext.jsf");

		// <view-id>/desktop/usercontext.jsp</view-id>
		Element view_id = new Element("view-id");
		view_id.addContent("/desktop/usercontext.jsp");

		user_context.addContent(user_context_class);
		user_context.addContent(business_process);
		user_context.addContent(configure_view_id);
		user_context.addContent(view_id);

		menu.addContent(user_context);

	}

}
